/**
 * @file 请求中间件
 * @author haoxubin
 */
import Vue from 'vue';
import VueResource from 'vue-resource';
import {API_ROOT} from '@/conf/appInfo';
import {getCookie, signOut, isLogin} from '@/auth/authService';

Vue.use(VueResource);
/* eslint-disable */
Vue.http.options.crossOrigin = true;
Vue.http.options.credentials = true;
// Vue.http.options.emulateJSON = true;

Vue.http.interceptors.push((request, next) => {
    // 请求体进行处理
    console.log('请求中间件，配置token');
    request.headers = request.headers || {};
    if (isLogin()) {
        request.headers.set('Authorization', 'Bearer ' + getCookie('token'));
    }
    next((response) => {
        // 对响应的结果进行处理
        if (response.status === 401) {
            signOut();
        }
    })
});

console.log(API_ROOT + 'auth{/id}');
export const AuthResource = Vue.resource(API_ROOT + 'auth{/id}');
export const UserResource = Vue.resource(API_ROOT + 'users{/id}');
export const ArticleResource = Vue.resource(API_ROOT + 'article{/id}{/paramsId}');
export const TagsResource = Vue.resource(API_ROOT + 'tags{/id}{/paramsId}');

